//
//  MPWebViewController.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/10.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPWebViewController.h"
#import "RDVTabBarController.h"

@interface MPWebViewController () <UIWebViewDelegate>
@property (nonatomic, strong)UIWebView* webView;
@property (nonatomic, copy)NSString* url;

@end

@implementation MPWebViewController

- (instancetype)initWithTitle:(NSString *)title url:(NSString *)url
{
    if (self = [super init]) {
        self.title = title;
        self.url = url;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.rdv_tabBarController.tabBarHidden = YES;

    [self.view addSubview:self.webView];
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.bottom.mas_equalTo(self.view);
    }];
    
    [self loadWebData];
}

- (void)loadWebData
{
    if(self.url){
        KDLogInfo(@"webview get url %@", self.url);
        NSURL* loadingURL =[NSURL URLWithString:self.url];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:loadingURL];
        request.cachePolicy = NSURLRequestReloadIgnoringCacheData;
        [self.webView loadRequest:request];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - property
- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc] init];
        _webView.delegate = self;
    }
    return _webView;
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [[MPRemindHelper sharedInstance] showLoadingHudOnView:self.navigationController.view];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [[MPRemindHelper sharedInstance] hideHUD];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [[MPRemindHelper sharedInstance] showHudOnView:self.navigationController.view withTitleText:error.localizedDescription withDuration:1.0f];
}


@end
